library(magrittr)
library(plyr)
library(tidyverse)
library(readxl)
library(gridExtra)
library(broom)


rm(list=ls())
home = 'C:/Users/Jason/Dropbox/VNA_Responsiveness/Analysis/JOP-dataverse/'


permutation.results = paste0(home, 'RI-analyses.Rds') %>%
  readRDS
experimental.results = paste0(home, 'experimental-analyses.Rds') %>%
  readRDS


dv_survey = paste0(home, 'survey-outcomes.xlsx') %>%
  read_xlsx %>%
  mutate(Treatment=factor(x=Treatment, 
                          levels=c('Control',
                                   'Citizen',
                                   'Firm')),
         Missing=as.integer(is.na(Q1))) %>%
  subset(!is.na(Treatment))


significance = subset(permutation.results, 
                      Result %in% c('fig4a','fig4b') & term!='(Intercept)') %>%
  ddply(.variables='Result', 
        .fun=function(x) {
    est = subset(experimental.results, 
                 Result==x$Result[1] & term==x$term[1])$estimate
    ris = x$estimate
    qest = ecdf(ris)
    Q = round(qest(est), digits=3)
    data.frame(term=x$term[1],
               Result=x$Result[1],
               significance=Q)
  }, .progress='text', .inform=T)


fig4a = lm(Missing ~ Citizen, 
           data=subset(dv_survey, 
                       Firm==0)) %>%
  tidy %>%
  mutate(Result='fig4a')
fig4b = lm(Missing ~ Firm, data=
             subset(dv_survey, 
                    Citizen==0)) %>%
  tidy %>%
  mutate(Result='fig4b')


fig_2_a = ggplot(data=dv_survey, 
                 aes(x=Citizen, y=Missing)) +
  geom_point(size=2, alpha=0.25, position=position_jitter(width=0.2, height=0.2)) +
  geom_abline(intercept=subset(permutation.results, 
                               Result=='fig4a' & term=='(Intercept)')$estimate,
              slope=subset(permutation.results, 
                           Result=='fig4a' & term=='Citizen')$estimate, 
              color='black', alpha=0.01) +
  geom_abline(intercept=subset(fig4a, 
                               term=='(Intercept)')$estimate, 
              slope=subset(fig4a, 
                           term=='Citizen')$estimate, 
              color='#e63e40', linetype=2, size=1) +
  geom_text(data=data.frame(X=-0.2, 
                            Y=0.6, 
                            Text=paste0('p=', format(x=subset(significance, term=='Citizen')$significance, 
                                                     scientific=F)),
                            stringsAsFactors=F),
            aes(x=X, y=Y, label=Text), 
            size=3, hjust=0, inherit.aes=F) +
  scale_x_continuous(breaks=c(0,
                              1), 
                     labels=c('Control\n',
                              'Citizen\n')) +
  scale_y_continuous(breaks=c(0,
                              1), 
                     labels=c('\u00ACMissing',
                              'Missing')) +
  coord_fixed(ratio=1) +
  labs(x=NULL, y=NULL) +
  theme_minimal() +
  theme(axis.text.x=element_text(size=7),
        axis.text.y=element_text(size=7, angle=90, hjust=0.5, vjust=0.5),
        panel.border=element_rect(color='black', fill=NA))


fig_2_b = ggplot(data=dv_survey, 
                 aes(x=Firm, y=Missing)) +
  geom_point(size=2, alpha=0.25, position=position_jitter(width=0.2, height=0.2)) +
  geom_abline(intercept=subset(permutation.results, 
                               Result=='fig4b' & term=='(Intercept)')$estimate,
              slope=subset(permutation.results, 
                           Result=='fig4b' & term=='Firm')$estimate, 
              color='black', alpha=0.01) +
  geom_abline(intercept=subset(fig4b, 
                               term=='(Intercept)')$estimate, 
              slope=subset(fig4b, 
                           term=='Firm')$estimate, 
              color='#e63e40', linetype=2, size=1) +
  geom_text(data=data.frame(X=-0.2, 
                            Y=0.6, 
                            Text=paste0('p=', format(x=subset(significance, term=='Firm')$significance, 
                                                     scientific=F)),
                            stringsAsFactors=F),
            aes(x=X, y=Y, label=Text), 
            size=3, hjust=0, inherit.aes=F) +
  scale_x_continuous(breaks=c(0,
                              1), 
                     labels=c('Control\n',
                              'Firm\n')) +
  scale_y_continuous(breaks=c(0,
                              1), 
                     labels=c(' ',
                              ' ')) +
  coord_fixed(ratio=1) +
  labs(x=NULL, y=NULL) +
  theme_minimal() +
  theme(axis.text=element_text(size=7),
        axis.text.y=element_text(angle=90, hjust=0.5, vjust=0.5),
        panel.border=element_rect(color='black', fill=NA))


lm_fulltime = lm(Missing ~ FullTime, 
                 data=subset(dv_survey, 
                             !is.na(Treatment)))
fig_2_c = ggplot(data=dv_survey, 
                 aes(x=FullTime, y=Missing)) +
  geom_point(size=2, alpha=0.25, position=position_jitter(width=0.2, height=0.2)) +
  geom_abline(intercept=coef(lm_fulltime)[1], 
              slope=coef(lm_fulltime)[2], 
              color='#e63e40', linetype=2, size=1) +
  geom_text(data=data.frame(X=-0.2, 
                            Y=0.6, 
                            Text=paste0('p=', format(x=round(summary(lm_fulltime)$coefficients['FullTime','Pr(>|t|)'], 3), 
                                                     scientific=F)),
                            stringsAsFactors=F),
            aes(x=X, y=Y, label=Text), 
            size=3, hjust=0, inherit.aes=F) +
  scale_x_continuous(breaks=c(0,
                              1), 
                     labels=c('Part time\n',
                              'Full time\n')) +
  scale_y_continuous(breaks=c(0,
                              1), 
                     labels=c(' ',
                              ' ')) +
  coord_fixed(ratio=1) +
  labs(x=NULL, y=NULL) +
  theme_minimal() +
  theme(axis.text=element_text(size=7),
        axis.text.y=element_text(angle=90, hjust=0.5, vjust=0.5),
        panel.border=element_rect(color='black', fill=NA))


lm_centnom = lm(Missing ~ CentNom, 
                data=subset(dv_survey, 
                            !is.na(Treatment)))
fig_2_d = ggplot(data=dv_survey, 
                 aes(x=FullTime, y=Missing)) +
  geom_point(size=2, alpha=0.25, position=position_jitter(width=0.2, height=0.2)) +
  geom_abline(intercept=coef(lm_centnom)[1], 
              slope=coef(lm_centnom)[2], 
              color='#e63e40', linetype=2, size=1) +
  geom_text(data=data.frame(X=-0.2, 
                            Y=0.6, 
                            Text='p=0.000',
                            stringsAsFactors=F),
            aes(x=X, y=Y, label=Text), 
            size=3, hjust=0, inherit.aes=F) +
  scale_x_continuous(breaks=c(0,
                              1), 
                     labels=c('Local- or self-\nnominated',
                              'Centrally-\nnominated')) +
  scale_y_continuous(breaks=c(0,
                              1), 
                     labels=c(' ',
                              ' ')) +
  coord_fixed(ratio=1) +
  labs(x=NULL, y=NULL) +
  theme_minimal() +
  theme(axis.text=element_text(size=7),
        axis.text.y=element_text(angle=90, hjust=0.5, vjust=0.5),
        panel.border=element_rect(color='black', fill=NA))


lm_competitive = lm(Missing ~ Competitive, 
                    data=subset(dv_survey, 
                                !is.na(Treatment)))
fig_2_e = ggplot(data=dv_survey, 
                 aes(x=Competitive, y=Missing)) +
  geom_point(size=2, alpha=0.25, position=position_jitter(width=0.2, height=0.2)) +
  geom_abline(intercept=coef(lm_competitive)[1], 
              slope=coef(lm_competitive)[2], 
              color='#e63e40', linetype=2, size=1) +
  geom_text(data=data.frame(X=-0.2, 
                            Y=0.6, 
                            Text=paste0('p=', format(x=round(summary(lm_competitive)$coefficients['Competitive','Pr(>|t|)'], 3), 
                                                     scientific=F)),
                            stringsAsFactors=F),
            aes(x=X, y=Y, label=Text), 
            size=3, hjust=0, inherit.aes=F) +
  scale_x_continuous(breaks=c(0,
                              1), 
                     labels=c('Uncompetitively\nelected',
                              'Competitively\nelected')) +
  scale_y_continuous(breaks=c(0,
                              1), 
                     labels=c(' ',
                              ' ')) +
  coord_fixed(ratio=1) +
  labs(x=NULL, y=NULL) +
  theme_minimal() +
  theme(axis.text=element_text(size=7),
        axis.text.y=element_text(angle=90, hjust=0.5, vjust=0.5),
        panel.border=element_rect(color='black', fill=NA))


fig_2 = grid.arrange(fig_2_a, fig_2_b, fig_2_c, fig_2_d, fig_2_e, nrow=1)
ggsave('appendix-figure-10-1.png', fig_2, path=home, width=8, height=2, units='in')
ggsave('appendix-figure-10-1.tiff', fig_2, path=home, width=8, height=2, units='in')
